/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { imageData } from "./imageData.js";
import { sha1 } from "./sha1.js";

let content = 'ABCDEFGHIJKabcdefj123456+/AA==!@一段中文';
let contentArray = []
let uint8Array = new Uint8Array(imageData)
let content1k = ""
let content5k = ""
let contentArray1k = []
let contentArray5k = []
for (let i = 0; i < content.length; i++) {
    contentArray.push(content[i]);
  }
let dic = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
for (let i = 0; i < 1024; i++) {
    content1k += dic[i / 10]
    contentArray1k.push(dic[i / 10])
}
let num = 5120
for (let i = 0; i < num; i++) {
    content5k += dic[i / 10]
    contentArray5k.push(dic[i / 10])
}

function sha1_Test_01() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = sha1(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1(string) error'
    }
    print(`sha1_sha1(string): ${endTime - startTime} ms`)
}

function sha1_Test_02() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = sha1(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1(string1k) error'
    }
    print(`sha1_sha1(string1k): ${endTime - startTime} ms`)
}

function sha1_Test_03() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = sha1(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1(string5k) error'
    }
    print(`sha1_sha1(string5k): ${endTime - startTime} ms`)
}

function sha1_Test_04() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = sha1(contentArray)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1(Array) error'
    }
    print(`sha1_sha1(Array): ${endTime - startTime} ms`)
}

function sha1_Test_05() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = sha1(contentArray1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1(Array1k) error'
    }
    print(`sha1_sha1(Array1k): ${endTime - startTime} ms`)
}

function sha1_Test_06() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = sha1(contentArray5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1(Array5k) error'
    }
    print(`sha1_sha1(Array5k): ${endTime - startTime} ms`)
}

function sha1_Test_07() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = sha1(uint8Array)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1(uint8Array) error'
    }
    print(`sha1_sha1(uint8Array): ${endTime - startTime} ms`)
}

function sha1_Test_08() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = sha1.hex(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.hex(content) error'
    }
    print(`sha1_hex(string): ${endTime - startTime} ms`)
}

function sha1_Test_09() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = sha1.hex(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.hex(content1k) error'
    }
    print(`sha1_hex(string1k): ${endTime - startTime} ms`)
}

function sha1_Test_10() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = sha1.hex(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.hex(content5k) error'
    }
    print(`sha1_hex(string5k): ${endTime - startTime} ms`)
}

function sha1_Test_11() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = sha1.array(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.array(content) error'
    }
    print(`sha1_array(string): ${endTime - startTime} ms`)
}

function sha1_Test_12() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = sha1.array(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.array(content1k) error'
    }
    print(`sha1_array(string1k): ${endTime - startTime} ms`)
}

function sha1_Test_13() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = sha1.array(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.array(content) error'
    }
    print(`sha1_array(string5k): ${endTime - startTime} ms`)
}

function sha1_Test_14() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = sha1.digest(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.digest(content) error'
    }
    print(`sha1_digest(string): ${endTime - startTime} ms`)
}

function sha1_Test_15() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = sha1.digest(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.digest(content1k) error'
    }
    print(`sha1_digest(string1k): ${endTime - startTime} ms`)
}

function sha1_Test_16() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = sha1.digest(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.digest(content) error'
    }
    print(`sha1_digest(string5k): ${endTime - startTime} ms`)
}

function sha1_Test_17() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = sha1.arrayBuffer(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.arrayBuffer(content) error'
    }
    print(`sha1_arrayBuffer(string): ${endTime - startTime} ms`)
}

function sha1_Test_18() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = sha1.arrayBuffer(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.arrayBuffer(content1k) error'
    }
    print(`sha1_arrayBuffer(string1k): ${endTime - startTime} ms`)
}

function sha1_Test_19() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = sha1.arrayBuffer(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'sha1.arrayBuffer(content) error'
    }
    print(`sha1_arrayBuffer(string5k): ${endTime - startTime} ms`)
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    sha1_Test_01()
    sha1_Test_02()
    sha1_Test_03()
    sha1_Test_04()
    sha1_Test_05()
    sha1_Test_06()
    sha1_Test_07()
    sha1_Test_08()
    sha1_Test_09()
    sha1_Test_10()
    sha1_Test_11()
    sha1_Test_12()
    sha1_Test_13()
    sha1_Test_14()
    sha1_Test_15()
    sha1_Test_16()
    sha1_Test_17()
    sha1_Test_18()
    sha1_Test_19()
}

ArkTools.waitAllJitCompileFinish();

sha1_Test_01()
sha1_Test_02()
sha1_Test_03()
sha1_Test_04()
sha1_Test_05()
sha1_Test_06()
sha1_Test_07()
sha1_Test_08()
sha1_Test_09()
sha1_Test_10()
sha1_Test_11()
sha1_Test_12()
sha1_Test_13()
sha1_Test_14()
sha1_Test_15()
sha1_Test_16()
sha1_Test_17()
sha1_Test_18()
sha1_Test_19()